using Microsoft.EntityFrameworkCore;
using DeathReportManagement.Data;
using DeathReportManagement.Models;

namespace DeathReportManagement.Services;

public class DictionaryService
{
    private readonly DeathReportContext _context;
    
    public DictionaryService(DeathReportContext context)
    {
        _context = context;
    }
    
    public async Task<List<Dictionary>> GetByTypeAsync(string type)
    {
        return await _context.Dictionaries
            .Where(d => d.Type == type && d.IsActive)
            .OrderBy(d => d.SortOrder)
            .ThenBy(d => d.Name)
            .ToListAsync();
    }
    
    public async Task<Dictionary?> GetByCodeAsync(string type, string code)
    {
        return await _context.Dictionaries
            .FirstOrDefaultAsync(d => d.Type == type && d.Code == code && d.IsActive);
    }
}